import { defineStore } from 'pinia'
import { ref } from 'vue'
import { usePluginLibraryStore } from './pluginLibrary'
import { useConfigEditorStore } from './configEditor'
import { useGlobalValuesStore } from './globalValues'
import { useInfoDialogStore } from './infoDialog'
import { useLanguageStore } from './language'
import { agencyAsync } from '@/appox/appox'
import { appoxEnums } from '@/appox/appox'

export const useToolPanelStore = defineStore('tool', () => {
  // 状态
  const isConfigBusProtocolAvailable = ref(false)
  const isUpdateLicenseAvailable = ref(false)
  const isConfigGlobalPathAvailable = ref(false)
  const isUpdatingButtons = ref(false)
  
  // 按钮状态
  const isPluginLibraryEnabled = ref(false)
  const isConfigEditorEnabled = ref(false)
  const isGlobalValuesEnabled = ref(false)
  const isConfigBusProtocolEnabled = ref(false)
  const isUpdateLicenseEnabled = ref(false)
  const isConfigGlobalPathEnabled = ref(false)

  // 动作
  async function initializeToolPanel() {
    // 初始化工具面板可用性
    await updateToolPanelAvailability()
  }

  async function updateToolPanelAvailability() {
    isConfigBusProtocolAvailable.value = await agencyAsync.getConsoleClassInfo("config-bus-protocol.consoleworkflow") != null
    isUpdateLicenseAvailable.value = await agencyAsync.getConsoleClassInfo("update-license.consoleworkflow") != null
    isConfigGlobalPathAvailable.value = await agencyAsync.getConsoleClassInfo("config-global-path.consoleworkflow") != null
  }

  async function updateToolPanelButtons() {
    if (isUpdatingButtons.value) return
    isUpdatingButtons.value = true

    try {
      const status = await agencyAsync.getAppStatus()
      const isIdle = status === appoxEnums.ApplicationStatus.Idle

      isPluginLibraryEnabled.value = isIdle
      isConfigEditorEnabled.value = isIdle
      isGlobalValuesEnabled.value = isIdle
      isConfigBusProtocolEnabled.value = isIdle && isConfigBusProtocolAvailable.value
      isUpdateLicenseEnabled.value = isUpdateLicenseAvailable.value
      isConfigGlobalPathEnabled.value = isIdle && isConfigGlobalPathAvailable.value
    } finally {
      isUpdatingButtons.value = false
    }
  }

  async function showPluginLibrary() {
    const pluginLibraryStore = usePluginLibraryStore()
    await pluginLibraryStore.show()
  }

  async function showConfigEditor() {
    const configEditorStore = useConfigEditorStore()
    await configEditorStore.show()
  }

  async function showGlobalValues() {
    const globalValuesStore = useGlobalValuesStore()
    await globalValuesStore.show()
  }

  async function configBusProtocol() {
    await agencyAsync.runConsole('config-bus-protocol.consoleworkflow')
  }

  async function updateLicense() {
    await agencyAsync.runConsole('update-license.consoleworkflow')
  }

  async function configGlobalPath() {
    await agencyAsync.runConsole('config-global-path.consoleworkflow')
  }

  async function showVersions() {
    const versions = await agencyAsync.getVersionTable()
    if (versions) {
      const rows = []
      for (const key in versions) {
        rows.push(`${key}: ${versions[key]}`)
      }
      const infoDialogStore = useInfoDialogStore()
      const languageStore = useLanguageStore()
      await infoDialogStore.show(languageStore.isChinese ? '版本' : 'Versions', rows.join('\n'))
    }
  }

  async function showLicenseInfo() {
    const info = await agencyAsync.getLicenseInfo()
    if (info) {
      const infoDialogStore = useInfoDialogStore()
      const languageStore = useLanguageStore()
      await infoDialogStore.show(languageStore.isChinese ? '许可证信息' : 'License info', info)
    }
  }

  async function showGpuDecoderInfo() {
    const results = await agencyAsync.getGPUDecoderTestResults()
    if (results) {
      const rows = []
      for (const key in results.testResults) {
        const value = results.testResults[key]
        rows.push(`${key}: ${value === null ? '---' : value}`)
      }
      const infoDialogStore = useInfoDialogStore()
      const languageStore = useLanguageStore()
      await infoDialogStore.show(languageStore.isChinese ? 'GPU解码器信息' : 'GPU decoder info', rows.join('\n'))
    }
  }

  return {
    // 状态
    isConfigBusProtocolAvailable,
    isUpdateLicenseAvailable,
    isConfigGlobalPathAvailable,
    isUpdatingButtons,
    // 按钮状态
    isPluginLibraryEnabled,
    isConfigEditorEnabled,
    isGlobalValuesEnabled,
    isConfigBusProtocolEnabled,
    isUpdateLicenseEnabled,
    isConfigGlobalPathEnabled,
    // 动作
    initializeToolPanel,
    updateToolPanelAvailability,
    updateToolPanelButtons,
    showPluginLibrary,
    showConfigEditor,
    showGlobalValues,
    configBusProtocol,
    updateLicense,
    configGlobalPath,
    showVersions,
    showLicenseInfo,
    showGpuDecoderInfo
  }
})